package com.liam.rpc.client;

import com.liam.rpc.server.RpcRequest;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;


/**
 * 代理
 * 完成网络通信逻辑
 */
@Getter
@Setter
@AllArgsConstructor
public class RemoteInvocationHandler implements InvocationHandler {

    private String host;
    private int port;


    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        RpcRequest request = new RpcRequest();
        request.setClassName(method.getDeclaringClass().getName());
        request.setMethodName(method.getName());
        request.setParameters(method.getParameters());
        request.setTypes(method.getParameterTypes());

        RpcNetTransport transport = new RpcNetTransport(host,port);
        return transport.sendRequest(request);

    }
}
